package com.hotec.PROJECT_NAME.utils;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hotec.PROJECT_NAME.component.exception.exc.BizException;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

@Slf4j
public class ImageBase64Converter {
    /**
     * 本地文件（图片、excel等）转换成Base64字符串
     *
     * @param imgPath     
     */
    public static String convertFileToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        InputStream in = null;

        try {
            in = new FileInputStream(imgPath);
            System.out.println("文件大小（字节）="+in.available());
            data = IoUtil.readBytes(in);
            in.close();
        } catch (IOException e) {
            UtilsLog.log(ImageBase64Converter.class, e);
        }finally {
            IoUtil.close(in);
        }

        if(ObjectUtil.isNull(data)) return null;

        // 对字节数组进行Base64编码，得到Base64编码的字符串
        return Base64Encoder.encode(data);
    }

    /**
     * 将base64字符串，生成文件
     */
    public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
                dir.mkdirs();
            }

            byte[] bfile = Base64Decoder.decode(fileBase64String);

            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
            return file;
        } catch (Exception e) {
            UtilsLog.log(ImageBase64Converter.class, e);
            return null;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    UtilsLog.log(ImageBase64Converter.class, e1);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    UtilsLog.log(ImageBase64Converter.class, e1);
                }
            }
        }
    }
    public static String encodeImgageToBase64(String remark) {

        log.info("into9 data" + remark);
        ByteArrayOutputStream outputStream = null;

        try {

            URL url = new URL(remark);

            BufferedImage bufferedImage = ImageIO.read(url);

            outputStream = new ByteArrayOutputStream();

            ImageIO.write(bufferedImage,"jpg",outputStream);

            return Base64Encoder.encode(outputStream.toByteArray());
        } catch (IOException e) {
            UtilsLog.log(ImageBase64Converter.class, e);

           throw new BizException("图片下载失败");
        }
    }

}